package com.situ.object.controll;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.situ.object.common.ResponseBean;
import com.situ.object.entity.User;
import com.situ.object.service.UserService;
import com.situ.object.service.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/modifiedPwd")
public class ModifiedPwdServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //一、设置req编码格式
        req.setCharacterEncoding("utf-8");
        //设置内容类型
        resp.setContentType("application/json;charset=utf-8");

        //二、获取参数(旧密码，新密码，确认密码)
        String password = req.getParameter("password");
        String newPassword = req.getParameter("newPassword");
        String rePassword = req.getParameter("rePassword");

//        System.out.println(password);
//        System.out.println(newPassword);
//        System.out.println(rePassword);
        //三、调用service 首先合成类
        //获取user信息
        User user = (User) req.getSession().getAttribute("user");
        //调用service方法
        try {
            userService.modifiedPwdUser(user,password,newPassword,rePassword);
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.ok());
            resp.getWriter().append(jsonStr);
        } catch (Exception e) {
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.failed(e.getMessage()));
            resp.getWriter().append(jsonStr);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
